﻿using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Azure.Online.Web.Controllers {
    public class UtilsController : Controller {

        public ActionResult WikiToHtml(string wiki) {
            var content = string.Empty;
            wiki = wiki ?? "";

            var reader = new StringReader(wiki);
            var listOpen = false;
            while (reader.Peek() >= 0) {
                var line = reader.ReadLine();

                line = ReplaceUrls(line);
                line = ReplaceEm(line);
                if (string.IsNullOrWhiteSpace(line)) continue;

                if (line.StartsWith("* ")) {
                    line = line.Substring(2);
                    if (!listOpen) {
                        line = "<ul class='disc'>\r\n<li>" + line + "</li>";
                        listOpen = true;
                    } else {
                        line = "<li>" + line + "</li>";
                    }
                } else {
                    if (listOpen) {
                        content += "</ul>\r\n";
                        listOpen = false;
                    }
                }

                if (line.StartsWith("! ")) {
                    line = line.Substring(2);
                    line = "<h2>" + line + "</h2>";
                } else if (line.StartsWith("!! ")) {
                    line = line.Substring(3);
                    line = "<h5>" + line + "</h5>";
                } else {
                    if (!listOpen) {
                        line = "<p>" + line + "</p>";
                    }
                }
                content += line + "\r\n";

            }
            if (listOpen) {
                content += "</ul>\r\n";
            }
            return View("WikiToHtml", new WikiToHtmlModel {
                Content = content,
                Wiki = wiki
            });
        }

        private static string ReplaceEm(string content) {
            var index = 0;
            while (index < content.Length) {
                var startEmIndex = content.IndexOf("_", index, StringComparison.InvariantCulture);
                if (startEmIndex < 0) return content;
                index = startEmIndex + 1;
                var endEmIndex = content.IndexOf("_", index, StringComparison.InvariantCulture);
                if (endEmIndex < 0) return content;
                index = endEmIndex + 1;

                var left = content.Substring(0, startEmIndex);
                var right = content.Substring(endEmIndex + 1);
                var middle = content.Substring(startEmIndex + 1, endEmIndex - startEmIndex - 1);
                content = left + "<em>" + middle + "</em>" + right;
            };
            return content;
        }

        private static string ReplaceUrls(string content) {
            var index = 0;
            while (index < content.Length) {
                var startUrlIndex = content.IndexOf("[url:", index, StringComparison.InvariantCulture);
                if (startUrlIndex < 0) return content;
                index = startUrlIndex + 5;
                var endUrlIndex = content.IndexOf("]", index, StringComparison.InvariantCulture);
                if (endUrlIndex < 0) return content;
                index = endUrlIndex + 1;

                var left = content.Substring(0, startUrlIndex);
                var right = content.Substring(endUrlIndex + 1);
                var middle = content.Substring(startUrlIndex + 5, endUrlIndex - startUrlIndex - 5);
                content = left + "<a href='" + middle + "' target='_blank'>" + middle + "</a>" + right;
            };
            return content;
        }
    }

    public class WikiToHtmlModel {
        public string Content { get; set; }

        public string Wiki { get; set; }

        public string Encoded {
            get {
                var c = Content ?? "";
                var lines = c.Split(new[] { "\r\n" }, StringSplitOptions.None);
                lines = lines.Select(HttpUtility.HtmlEncode).ToArray();
                return string.Join("<br />", lines);
            }
        }
    }
}
